<?php


namespace App\Repositories\Log;


use App\Models\BaseModel;
use App\Models\Log\Operation;
use App\Repositories\BaseRepository;
use App\Utils\Output;
use App\Utils\Pager;
use App\Utils\QueryHelper;

class OperationRepository extends BaseRepository
{

    function adminPager($params)
    {
        extract($params);
        $query = Operation::query();

        // if (!empty($start)) {
        //     $query->where('log__operations.created_at', '>=', $start);
        // }
        if (!empty($date_range)) { 
            list($start, $end) = explode(' ~ ', $date_range);
            $query->whereBetween('log__operations.created_at', [$start, $end]);
           
        } 

        if (!empty($keyword)) {
            QueryHelper::like($query, 'system__users.username|log__operations.user_id', $keyword);
        }

        if (!empty($pv)) {
            QueryHelper::like($query, 'log__operations.params', $pv);
        }

        if (!empty($url)) {
            $query->where('log__operations.url', '=', $url);
        }

        if (!empty($session)) {
            $query->where('log__operations.session_id', '=', $session);
        }

        // if (!empty($end)) {
        //     $query->where('log__operations.created_at', '<=', $end . ' 23:59:59');
        // }
        $query->leftJoin('system__users', 'system__users.id', '=', 'log__operations.user_id');
        $query->leftJoin('system__hospitals', 'system__hospitals.id', '=', 'log__operations.hospital_id');
        $total = $query->count();
        $query->select(
            'log__operations.id',
            'log__operations.method',
            'log__operations.server_addr',
            'log__operations.ip',
            'log__operations.time',
            'log__operations.session_id',
            'log__operations.url',
            'log__operations.user_id',
            'system__users.username as username',
            'system__hospitals.name as hospital_name',
            'system__users.name as user_name',
            'system__users.mobile as user_mobile',
        );
        $lis = Pager::setPageParams($query, $params)->get();

        return Output::pager($lis, $total);
    }

    function detailInfo($id)
    {
        $log = Operation::find($id);
        $logs = Operation::query()
            ->leftJoin('system__users', 'system__users.id', '=', 'log__operations.user_id')
            ->leftJoin('system__hospitals', 'system__hospitals.id', '=', 'log__operations.hospital_id')
            ->select(
                'log__operations.id',
                'log__operations.time',
                'log__operations.ip',
                'log__operations.user_id',
                'log__operations.method',
                'log__operations.url',
                'system__users.username as username',
                'system__hospitals.name as hospital_name',
                'system__users.name as user_name',
                'system__users.mobile as user_mobile',
            )
            ->where('session_id', '=', $log['session_id'])->get();
        $user = false;
        foreach ($logs as $item) {
            if ($item->user_id && $user == false) {
                $user = \App\Models\System\User::find($item->user_id);
            }
        }
        return [
            'user' => $user,
            'session_id' => $log['session_id'],
            'logs' => $logs
        ];
    }

    function getModel(): BaseModel
    {
        return new Operation();
    }
}